
import * as util from '@/utils/util'

export const setAppPage = (appPage, app) => { 
	let addshowcase = false
	if (appPage.appTitle) {
		app.page.params.shareTitle = appPage.appTitle;
		app.page.params.title = appPage.appTitle;
	} 

	app.items.forEach(item => {
		if (item.type == 'navBar') {
			try {
			  setNavBar(appPage.categories, item)
			} catch(e) {
				item.data = [];
			}
		} else if (item.type == 'banner') {
			try {
			 setBanner(appPage.banners, item);
			} catch(e) {
				item.data = [];
			}
		} else if (item.type == 'goods') {
			try {
			   setGoods(appPage.goods, item)
			} catch(e) {
				item.data = [];
			}
		} else if (item.type == 'notice') {
			try {
			   setNotice(appPage.appNotice, item)
			} catch(e) {
			}
		} else if (item.type == 'image') {
			try {
				if (!addshowcase) {
				  setImageType(appPage.showcases, item)
				}
			} catch(e) {
				item.data = [];
			} finally {
				addshowcase = true;
			}
		}
	});
}


export const setNotice = (notice, pageItem) => { 
	if (!notice) {
		return;
	}
	pageItem.params.text = notice;
}

export const setGoods = (goodses, pageItem) => { 
	if (!goodses || goodses.length == 0) {
		return;
	}
	let dataArray = [];
	goodses.forEach(goods => { 
		let price = goods.price;
		if (goods.discountPrice && goods.discountPrice >=0.01) {
			price = goods.discountPrice;
		}
		let data = {
			goods_id: goods.id,
			goods_name: goods.name,
			goods_price_min: price,
			goods_sales: goods.saleNum,
			goods_image: util.attachPreviewUrl(goods.imageId),
		}
		dataArray.push(data);
	})
	pageItem.data = dataArray;
}


export const setBanner = (banners, pageItem) => { 
	if (!banners || banners.length == 0) {
		return;
	}
	let dataArray = [];
	banners.forEach(banner => { 
		let data =  {
			"imgUrl": util.attachPreviewUrl(banner.attachId),
			"link": {
				"id": banner.relatedId,
				"title": banner.name,
				"type": "PAGE",
				"param": {
					"path": "pages/goods/detail",
					"query": {
						"goodsId": banner.relatedId
					}
				},
				"form": [
					{
						"key": "query.goodsId",
						"lable": "商品ID",
						"required": true,
						"tips": "商品管理 - 商品列表",
						"value": banner.relatedId
					}
				]
			},
			"imgName": banner.name
		}
		dataArray.push(data);
	});
	pageItem.data = dataArray;
}



/**
 * 设置导航栏
 * @param {*} categories 
 * @param {*} pageItems 
 */
export const setNavBar = (categories, pageItem) => {
	if (!categories || categories.length == 0) {
		return;
	}
	let dataArray = [];
	categories.forEach(category => { 
		let imageUrl = '/static/default-logo.png'
		if (category.imageId) {
			imageUrl =  util.attachPreviewUrl(category.imageId);
		}
		let data = {
			imgName:category.name,
			text: category.name,
			imgUrl: imageUrl,
			"link": {
				"id": "995bf1c",
				"title": "商品列表页",
				"type": "PAGE",
				"param": {
					"path": "pages/goods/list",
					"query": {
						"categoryId": category.id,
						"search": ""
					}
				},
				"form": [
					{
						"key": "query.categoryId",
						"lable": "分类ID",
						"tips": "商品管理 -> 商品分类",
						"value": category.id
					},
					{
						"key": "query.search",
						"lable": "关键词",
						"tips": "搜索的关键词，用于匹配商品名称",
						"value": ""
					}
				]
			}
		}
		dataArray.push(data);
    })
	pageItem.data = dataArray;

}


export const setImageType = (showcases, pageItem) => { 
	if (!showcases || showcases.length == 0) {
		return;
	}

	showcases.forEach(showcase => { 
		let data =  {
			"imgUrl": util.attachPreviewUrl(showcase.attachId),
			"link": {
				"id": showcase.relatedId,
				"title": showcase.name,
				"type": "PAGE",
				"param": {
					"path": "pages/goods/list",
					"query": {
						"categoryId": showcase.relatedId
					}
				},
				"form": [
					{
						"key": "query.categoryId",
						"lable": "分类ID",
						"tips": "商品管理 -> 商品分类",
						"value": showcase.relatedId
					},
					{
						"key": "query.search",
						"lable": "关键词",
						"tips": "搜索的关键词，用于匹配商品名称",
						"value": ""
					}
				]
			},
			"imgName": showcase.name
		}
		pageItem.data.push(data);
	});
}
